package com.itheima.web;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.common.ParamPage;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dfs;
    @Autowired
    private CategoryService cs;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        int i = dishService.add(dishDto);
        if(i==-1){
            return R.error("菜品添加失败！");
        }
        if (i==0){
            return R.error("菜品口味添加失败！");
        }
        return R.success("添加成功！");
    }
    @GetMapping("/page")
    public R page(ParamPage paramPage){
        IPage<DishDto> page = new Page<>();
        IPage<Dish> dishIPage = dishService.findpage(paramPage);
        page.setTotal(dishIPage.getTotal());
        List<DishDto> list = dishIPage.getRecords().stream().map(aa -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(aa, dishDto);
            dishDto.setCategoryName(cs.findByCid(aa.getCategoryId()).getName());
            return dishDto;
        }).collect(Collectors.toList());
        page.setRecords(list);
        return R.success(page);
    }
    @GetMapping("/{id}")
    public R findByid(@PathVariable Long id){
        DishDto dishDto = dishService.findByid(id);
        if (dishDto!=null){
            return R.success(dishDto);
        }
        return R.error("查询失败！");
    }
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        if (dishService.update(dishDto)>0){
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        if (dishService.delete(ids)>0){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }
    @PostMapping("/status/{status}")
    public R updatastatus(@PathVariable int status,@RequestParam List<Long> ids){
        System.out.println(status);
        System.out.println(ids);
        if (dishService.updatestatus(status,ids)>0){
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }
    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        String o = (String) redisTemplate.opsForValue().get(categoryId + "_" + status);
        if (!StringUtils.isEmpty(o)){
            return R.success(JSON.parseObject(o,List.class));
        }
        List<Dish> list = dishService.findByCid(categoryId,status);
        List<DishDto> dtoList = list.stream().map(aa -> {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(aa, dto);
            dto.setFlavors(dfs.findByDid(aa.getId()));
            return dto;
        }).collect(Collectors.toList());
        if (list.size()>0){
            redisTemplate.opsForValue().set(categoryId + "_" + status,JSON.toJSONString(dtoList));
            return R.success(dtoList);
        }
        return R.error("查询失败！");
    }
    @GetMapping("/list1")
    public R list(String name){
        List<Dish> list = dishService.findByCid1(name);
        System.out.println(name);
        System.out.println(list);
        if (list.size()>0){
            return R.success(list);
        }
        return R.error("查询失败！");
    }
}
